﻿using MvcOSGi.Shell.Properties;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using UIShell.OSGi.Utility;
using BusinessLayer;

namespace MvcOSGi.Shell.Infra
{
    public class RequestInfo
    {
        public RequestInfo(HttpRequest request)
        {
            Request = request;
            Url = request.Url;
            FileLogUtility.Inform(string.Format("URL",Url));
            NameValueCollection defaultCollection = Settings.Default.URLRouting ? request.Headers : request.QueryString;
            CID = GetIntValue(defaultCollection, Enums.QueryVariable.CID) ?? 0;
            VersionID = GetIntValue(defaultCollection, Enums.QueryVariable.VersionID);
            UserHostAddress = request.UserHostAddress;
            AbsoluteUri = (Request.UrlReferrer == null) ? "" : Request.UrlReferrer.AbsoluteUri.ToString();
            UserAgent = Request.UserAgent;
            RawUrl = Request.RawUrl;
            RequestPageType = GetPageTypeID();
            FileLogUtility.Inform(string.Format("cid", CID));
            FileLogUtility.Inform(string.Format("versionid", VersionID));
        }

        public Uri Url { get; private set; }
        public int CID { get; private set; }
        public int? VersionID { get; private set; }
        public int? LocaleID { get; private set; }
        public string RequestPageType { get; private set; }
        public string UserHostAddress { get; private set; }
        public string AbsoluteUri { get; private set; }
        public string UserAgent { get; private set; }
        public string RawUrl { get; private set; }


        #region private
        private string GetPageTypeID()
        {
            var context = new HttpContextWrapper(HttpContext.Current);
            string pageName = context.GetPageType();
            return pageName == "" ? "index" : pageName;
        }

        private int? GetIntValue(NameValueCollection collection, Enums.QueryVariable queryVariable)
        {
            int result;
            return int.TryParse(GetValue(collection, queryVariable), out result) ? new int?(result) : null;
        }

        private bool GetBoolValue(NameValueCollection collection, Enums.QueryVariable queryVariable)
        {
            bool result;
            return bool.TryParse(GetValue(collection, queryVariable), out result) ? result : false;
        }

        private string GetValue(NameValueCollection collection, Enums.QueryVariable queryVariable)
        {
            string name = queryVariable.GetDescription();

            if (collection.GetType().FullName.EndsWith("HttpHeaderCollection"))
            {
                name = "p-" + name;
            }
            string[] values = collection.GetValues(name);

            return (!values.IsNullOrEmpty() && !string.IsNullOrWhiteSpace(values[0])) ? values[0].Trim() : string.Empty;
        }
        #endregion

        #region Public
        public string GetValue(Enums.QueryVariable queryVariable)
        {
            return GetValue(Request.QueryString, queryVariable);
        }


        public System.Web.Routing.RouteValueDictionary GetRouteValues()
        {
            NameValueCollection queryString = Request.QueryString;
            if (queryString == null || queryString.HasKeys() == false) return null;
            var routeValues = new System.Web.Routing.RouteValueDictionary();
            foreach (string key in queryString.AllKeys)
            {
                routeValues.Add(key, queryString[key]);
            }
            return routeValues;
        }
        #endregion

        #region Private Properties
        private HttpRequest Request { get; set; }
        #endregion
    }
}